package com.person.android.ui.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.person.android.domain.email.EMailSender;
import com.person.android.domain.email.EmailService;
import com.person.android.domain.IEmailService;
import com.person.android.formatts.R;
import com.person.android.utils.LogUtils;
import com.person.android.utils.NetWorkUtils;
import com.person.android.utils.ToastUtils;

/**
 * @autor zhaokang.
 * @time on 2015/12/18.
 */
public class FeedbackActivity extends Activity implements View.OnClickListener {

    private TextView mHeaderTv;
    private ImageView mBackImv;                 //返回按钮
    private RelativeLayout mHeaderContainer;    //顶部窗口容器

    private Button mSentBtn;                //反馈按钮
    private EditText mFeedbackEdt;          //反馈信息
    private ProgressDialog mSendDialog;     //反馈信息旋转框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        findViewsAndInitialUI();
        setListeners();
    }

    private void setListeners() {
        mSentBtn.setOnClickListener(this);
        mBackImv.setOnClickListener(this);
        mHeaderContainer.setOnClickListener(this);
    }

    private void findViewsAndInitialUI() {
        mSentBtn = (Button) findViewById(R.id.sendBtn);
        mBackImv = (ImageView) findViewById(R.id.ibv_back);
        mHeaderTv = (TextView) findViewById(R.id.header_txt);
        mHeaderTv.setText(R.string.activity_advice_feedback);
        mFeedbackEdt = (EditText) findViewById(R.id.feedback_edit);
        mHeaderContainer = (RelativeLayout) findViewById(R.id.header_container);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibv_back:
            case R.id.header_container:
                finish();
                break;
            case R.id.sendBtn:
                startSendFeedbackTask();
                break;
        }
    }

    //启动发送反馈信息任务
    private void startSendFeedbackTask() {
        if (NetWorkUtils.isNetworkAvailable(this)) {

            //IEmailService service = new EmailService();
            //service.startEmailService(this,"this is a test text");

            new Thread(){
                @Override
                public void run() {

                    LogUtils.logInfo(LogUtils.FileListActivityFlag,"thread start...");

                    String host = "stmp.qq.com";//"1063995011@qq.com";
                    String port = "25";
                    String userName = "1063995011";
                    String password = "zhaokang476400";
                    String subject = "test_demo";
                    String reception = "1063995011@qq.com";
                    String sender = "1063995011@qq.com";
                    String content = "this is an android email test";
                    new EMailSender(host, port, userName, password).sendEmail(subject, reception, sender, content);
                }
            }.start();
//            showSendFeedbackDialog();
//            new SendEmailTask("test@email.com").setart();
//            new SendFeedbackTask().execute();
        } else {
            ToastUtils.showToast(this, getString(R.string.network_is_not_available));
        }
    }

    //显示发送框
    private void showSendFeedbackDialog() {
        if (mSendDialog == null) {
            mSendDialog = ProgressDialog.show(this, "", getString(R.string.activity_advice_feedback_sending), true, false);
        }
    }

    //消失发送框
    private void dismissFeedbackDialog() {
        if (mSendDialog != null && mSendDialog.isShowing()) {
            mSendDialog.dismiss();
            mSendDialog = null;
        }
    }

    private final class SendFeedbackTask extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected Boolean doInBackground(Void... voids) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            dismissFeedbackDialog();
        }
    }


}
